package com.pick.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.pick.admin.entity.SysRole;
import com.pick.admin.service.SysRoleService;
import com.pick.base.common.PageQuery;
import com.pick.base.common.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色管理控制器
 */
@Tag(name = "角色管理")
@RestController
@RequestMapping("/system/role")
public class SysRoleController {

    @Autowired
    private SysRoleService roleService;

    @Operation(summary = "分页查询角色列表")
    @SaCheckPermission("system:role:list")
    @GetMapping("/page")
    public Result<IPage<SysRole>> pageRoles(PageQuery pageQuery) {
        IPage<SysRole> page = roleService.pageRoles(pageQuery);
        return Result.success(page);
    }

    @Operation(summary = "查询所有角色")
    @SaCheckPermission("system:role:list")
    @GetMapping("/list")
    public Result<List<SysRole>> listRoles() {
        List<SysRole> roles = roleService.list();
        return Result.success(roles);
    }

    @Operation(summary = "根据ID查询角色")
    @SaCheckPermission("system:role:query")
    @GetMapping("/{id}")
    public Result<SysRole> getRole(@PathVariable Long id) {
        SysRole role = roleService.getById(id);
        return Result.success(role);
    }

    @Operation(summary = "创建角色")
    @SaCheckPermission("system:role:add")
    @PostMapping
    public Result<String> createRole(@RequestBody SysRole role) {
        boolean success = roleService.createRole(role);
        return success ? Result.success("创建成功") : Result.error("创建失败");
    }

    @Operation(summary = "更新角色")
    @SaCheckPermission("system:role:edit")
    @PutMapping
    public Result<String> updateRole(@RequestBody SysRole role) {
        boolean success = roleService.updateRole(role);
        return success ? Result.success("更新成功") : Result.error("更新失败");
    }

    @Operation(summary = "删除角色")
    @SaCheckPermission("system:role:remove")
    @DeleteMapping("/{id}")
    public Result<String> deleteRole(@PathVariable Long id) {
        boolean success = roleService.deleteRole(id);
        return success ? Result.success("删除成功") : Result.error("删除失败");
    }

    @Operation(summary = "分配权限")
    @SaCheckPermission("system:role:permission")
    @PutMapping("/assignPermissions")
    public Result<String> assignPermissions(@RequestBody AssignPermissionsRequest request) {
        boolean success = roleService.assignPermissions(request.getRoleId(), request.getPermissionIds());
        return success ? Result.success("分配成功") : Result.error("分配失败");
    }

    @Operation(summary = "获取角色权限ID列表")
    @SaCheckPermission("system:role:query")
    @GetMapping("/{id}/permissions")
    public Result<List<Long>> getRolePermissions(@PathVariable Long id) {
        List<Long> permissionIds = roleService.getRolePermissionIds(id);
        return Result.success(permissionIds);
    }

    /**
     * 分配权限请求参数
     */
    @Data
    public static class AssignPermissionsRequest {
        private Long roleId;
        private List<Long> permissionIds;
    }
}